package com.vtuzx.kingdee.xqc_core.dao;

import com.vtuzx.core.db.mongo.impl.VtuzxDaoImpl;
import org.bson.Document;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Component
public class SelfProductBillDao extends VtuzxDaoImpl {
    private static final String COLLECTION_NAME = "t_self_product_bill";

    public SelfProductBillDao() {
        super(COLLECTION_NAME);
    }

    /**
     * 插入自采品单据
     */
    public void insertSelfProductBill(String billNo, String selfProductNo) {
        Document bill = new Document()
                .append("billNo", billNo)
                .append("selfProductNo", selfProductNo)
                .append("createdTime", new Date());
        insertOne(bill);
    }

    /**
     * 检查调拨单是否包含自采品
     */
    public boolean isSelfProductBill(String billNo) {
        Criteria criteria = Criteria.where("billNo").is(billNo);
        return count(criteria) > 0;
    }

    /**
     * 获取销售单的自采品列表
     */
    public List<String> getSelfProductsByTradeNo(String tradeNo) {
        Criteria criteria = Criteria.where("tradeNo").is(tradeNo);
        Iterator<Document> iter = findList(criteria, null, 0, 0);
        List<String> productIds = new ArrayList<>();
        while (iter.hasNext()) {
            productIds.add(iter.next().getString("productId"));
        }
        return productIds;
    }
}